import Request from 'utils/request';

import GoodsOrderData from 'models/goodsOrder/goodsOrderData';

const payType = {
  toPay: 0
};

export default class GoodsOrderApi {
  static getOrderList(type, pagination, isKeep = false) {
    const apiName = 'api/wehcatshop/getMineOrderInfo';

    let postData;
    if (isKeep) {
      let count = pagination.currentPage * pagination.pageSize;
      postData = {
        limit: count,
        page: 1
      };
    } else {
      postData = {
        limit: pagination.pageSize,
        page: pagination.currentPage
      };
    }

    if (type !== 'all') {
      postData.status = payType[type];
    }

    return Request.post(apiName, postData).then(res => {
      let data = res.data;
      if (data === null) {
        data = [];
      }
      let orderList = [];
      for (let i = 0; i < data.length; i++) {
        let order = data[i];
        order = new GoodsOrderData(order);
        orderList.push(order);
      }

      pagination.totalCount = res.rows;
      return orderList;
    });
  }

  static applyRetreatOrder(goods, reason) {
    const apiName = 'wechatpay/orderRefuseApply';

    let postData = goods.getApplyRetreatObject(reason);

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static approvalRetreatOrder(goods) {
    const apiName = 'wechatpay/orderRefuseAudit';

    let postData = {
      orderRefuseApplyId: goods.orderRefuseApplyId,
      status: 1
    };

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static submitGoodEvaluat(goodsEval) {
    const apiName = 'api/wehcatshop/goodsAppraiseAdd';
    let postData = goodsEval.getObject();

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }
}
